package com.ray.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ray.common.utils.Constast;
import com.ray.common.utils.Result;
import com.ray.web.entity.DataGridView;
import com.ray.web.entity.Loginfo;
import com.ray.web.service.LoginfoService;
import com.ray.web.vo.LoginfoVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ray
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/sys/loginfo")
public class LoginfoController {

    @Autowired
    private LoginfoService loginfoService;

    /**
     * 全查询
     */
    @GetMapping("getLoginfoList")
    public DataGridView getLoginfoList(LoginfoVo loginfoVo){
        IPage<Loginfo> page = new Page<>(loginfoVo.getPage(), loginfoVo.getLimit());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(StringUtils.isNotEmpty(loginfoVo.getLoginName()), "login_name",loginfoVo.getLoginName());
        //ge大于等于  gt大于
        wrapper.ge(loginfoVo.getStartTime()!=null,"login_time",loginfoVo.getStartTime() );
        //le小于等于  lt小于
        wrapper.ge(loginfoVo.getEndTime()!=null,"login_time",loginfoVo.getEndTime() );
        wrapper.orderByDesc("login_time");
        loginfoService.page(page,  wrapper);
        return new  DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 删除
     */
    @PostMapping("deleteLoginfo")
    public Result deleteLoginfo(Integer id){

        try {
            loginfoService.removeById(id);
            return Result.success(Constast.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(Constast.DELETE_ERROR);
        }
    }

    /**
     * 批量删除
     */
    @PostMapping("batchDeleteLoginfo")
    public Result batchDeleteLoginfo(LoginfoVo loginfoVo){
        try {
            Collection<Serializable> idList = new ArrayList<>();
            for (Integer id : loginfoVo.getIds()) {
                idList.add(id);
            }
            loginfoService.removeByIds(idList);
            return Result.success(Constast.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(Constast.DELETE_ERROR);
        }
    }

}
